package com.haojiangbo.config;

import com.haojiangbo.start.ClientAppStart;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author WangNing
 * @version 1.0
 * @date 2021/1/2 14:52
 */
@Slf4j
public class KeepAliveThread extends Thread{

    public static AtomicLong lastConnect = new AtomicLong();

    @Override
    public void run() {
        log.info("保活程序启动");
        while (true){
            if(Thread.interrupted()){
                break;
            }
            try{
                Thread.sleep(1000 * 60 * 10);
            }catch (InterruptedException e){
                break;
            }

            if(lastConnect.get() - System.currentTimeMillis()> 1000 * 60 * 10){
                log.info("检测超过10分钟内没有时间触发，重启服务器");
                ClientAppStart.stopContainers();
                ClientAppStart.startContainers();
            }
        }

    }
}
